from itsdangerous import TimedJSONWebSignatureSerializer as ser

from meiduo.settings import dev

# 对openid加密
def generte_access_token(openid):

    serializer = ser(dev.SECRET_KEY,
                     expires_in=600)
    data = {'openid':openid}

    token = serializer.dumps(data).decode()
    return token


def check_access_token(access_token):
    '''把access_token解密为openid'''

    obj = ser(dev.SECRET_KEY, expires_in=600)

    try:
        data = obj.loads(access_token)
    except Exception as e:
        return None
    else:
        return data.get('openid')